package com.a3g.findnumber.entity;

import java.util.List;
import java.util.Random;

import android.content.Context;
import android.widget.Button;

public class Star extends Button {
	public static int IMAGE_WIDTH = 130;

	public static int IMAGE_HEIGHT = 130;

	private int imageId;

	private int starX = 0;

	private int starY = 0;

	public Star(Context context) {
		super(context);
	}

	public void initialStar(int imageId, int screenWidth, int screenHeight, List<Star> recentStars) {
		this.imageId = imageId;
		this.autoGenerateLocation(screenWidth, screenHeight, recentStars);
		// --- generate Star location
	}

	public void autoGenerateLocation(int screenWidth, int screenHeight, List<Star> recentStars) {
		do {
			this.generateLocation(screenWidth, screenHeight);

		} while (this.validateLocaltion(recentStars) != true);
	}

	private void generateLocation(int screenWidth, int screenHeight) {

		Random random = new Random();
		starX = random.nextInt(screenWidth - Star.IMAGE_WIDTH * 2) + Star.IMAGE_WIDTH;
		starY = random.nextInt(screenHeight - Star.IMAGE_HEIGHT * 2) + Star.IMAGE_HEIGHT;
	}

	public int getStarX() {
		return this.starX;
	}

	public int getStarY() {
		return this.starY;
	}

	public void resetLocation() {
		this.starX = 0;
		this.starY = 0;
	}

	public int getStarImage() {
		return this.imageId;
	}

	public boolean validateLocaltion(List<Star> stars) {
		boolean result = true;

		for (Star star : stars) {

			if (star != this) {
				result = this.checkLocationWithAnother(star);
			}

			// ---- If result == false =>> Break to return false result;
			if (result == false) {
				break;
			}
		}
		return result;
	}

	private boolean checkLocationWithAnother(Star star) {

		boolean result = false;

		//Change by duongza + 30
		if (Math.abs(this.starX - star.getStarX() + 20) <= Star.IMAGE_WIDTH
				&& Math.abs(this.starY - star.getStarY() + 30) <= Star.IMAGE_HEIGHT) {
			result = false;
		} else {
			result = true;
		}
		
		// end
		
		

		return result;
	}
}